import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
base_url = "http://www.kirgen.com/"

def get_product_detail():
    headers = {
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Cache-Control": "no-cache",
        "Pragma": "no-cache",
        "Proxy-Connection": "keep-alive",
        "Upgrade-Insecure-Requests": "1",
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36"
    }
    url = "http://www.kirgen.com/product/productdetail&spec=KG1011&pid=36"
    response = requests.get(url, headers=headers, verify=False)

    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'html.parser')
        return soup

def extract_product_img(bs4_soup):
    cpxq_boxes = bs4_soup.find_all('div', class_='cpxqBox1')
    
    if not cpxq_boxes:
        print("未找到class为'cpxqBox1'的div元素")
        return
    
    # 提取第一个图片链接
    for box in cpxq_boxes:
        # 在每个cpxqBox1中查找img标签
        img_tags = box.find_all('img')
        if img_tags:
            img = img_tags[0]  # 获取第一个img标签
            src = img.get('src')
            if src:
                # 如果提供了base_url且src是相对路径，则构建完整URL
                if base_url and not src.startswith(('http://', 'https://')):
                    src = urljoin(base_url, src)
                print(f"找到图片链接: {src}")
                return src
    
    print("未找到任何图片链接")
    return